package org.ldx.lotterysystem.service.activitystatus.operator;

import jakarta.annotation.Resource;
import org.ldx.lotterysystem.dao.dataobject.ActivityUserDo;
import org.ldx.lotterysystem.dao.mapper.ActivityUserMapper;
import org.ldx.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ldx.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 帘低晓露湿
 * @date 2025/11/13 11:03
 */
@Component

public class UserOperator extends AbstractActivityOperator{
    @Resource
    ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum activityUserStatusEnum = convertActivityStatusDTO.getTargetUserStatus();
        if(null == activityId||
                null == activityUserStatusEnum||
                CollectionUtils.isEmpty(userIds)){
            return false;
        }
        List<ActivityUserDo> activityUserDos = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if(CollectionUtils.isEmpty(activityUserDos)){
            return false;
        }
        for(ActivityUserDo auDo:activityUserDos){
            if(auDo.getStatus().equalsIgnoreCase(activityUserStatusEnum.name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum activityUserStatusEnum = convertActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,activityUserStatusEnum.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
